package net.zio.kamenriderzioaddon.network;

import net.minecraft.server.level.ServerPlayer;
import net.minecraft.world.entity.player.Player;
import net.minecraftforge.event.TickEvent;
import net.minecraftforge.event.entity.EntityJoinWorldEvent;
import net.minecraftforge.event.entity.player.PlayerEvent;
import net.minecraftforge.eventbus.api.SubscribeEvent;
import net.minecraftforge.fml.common.Mod;
import net.zio.kamenriderzioaddon.KamenRiderZioAddonMod;
import net.zio.kamenriderzioaddon.ridersystem.data.PlayerDataManager;

/**
 * 玩家数据事件处理类
 *
 * @author QianYunYi
 *
 * 该类负责处理玩家数据相关的事件，包括玩家每刻事件、玩家登录事件、玩家加入世界事件等。
 */
@Mod.EventBusSubscriber(modid = KamenRiderZioAddonMod.MODID)
public class PlayerDataEventHandler {

    /**
     * 玩家每刻事件处理方法
     *
     * @param event 玩家每刻事件
     */
    @SubscribeEvent
    public static void onPlayerTick(TickEvent.PlayerTickEvent event) {
        if (event.phase == TickEvent.Phase.END) {
            Player player = event.player;
            PlayerDataManager dataManager = PlayerDataManager.get(player);
            dataManager.tick(player);
        }
    }

    /**
     * 玩家登录事件处理方法
     *
     * @param event 玩家登录事件
     */
    @SubscribeEvent
    public static void onPlayerLoggedIn(PlayerEvent.PlayerLoggedInEvent event) {
        if (event.getPlayer() instanceof ServerPlayer player) {
            PlayerDataManager dataManager = PlayerDataManager.get(player);
            dataManager.loadFromPlayer(player);

            if (dataManager.needsStateRestore()) {
                //dataManager.restorePlayerState(player);
                dataManager.setNeedsStateRestore(false);
            }

            dataManager.sync(player);
        }
    }

    /**
     * 玩家重生事件处理方法
     *
     * @param event 玩家重生事件
     */
    @SubscribeEvent
    public static void onPlayerRespawn(PlayerEvent.PlayerRespawnEvent event) {
        if (event.getPlayer() instanceof ServerPlayer player) {
            PlayerDataManager dataManager = PlayerDataManager.get(player);
            dataManager.loadFromPlayer(player);

            if (dataManager.needsStateRestore()) {
                //dataManager.restorePlayerState(player);
                dataManager.setNeedsStateRestore(false);
            }

            dataManager.sync(player);
        }
    }

    /**
     * 实体加入世界事件处理方法
     *
     * @param event 实体加入世界事件
     */
    @SubscribeEvent
    public static void onEntityJoinWorld(EntityJoinWorldEvent event) {
        if (event.getEntity() instanceof Player player) {
            PlayerDataManager dataManager = PlayerDataManager.get(player);
            if (player instanceof ServerPlayer) {
                dataManager.loadFromPlayer(player);
                if (dataManager.needsStateRestore()) {
                   // dataManager.restorePlayerState(player);
                    dataManager.setNeedsStateRestore(false);
                }
            }
        }
    }

    /**
     * 玩家克隆事件处理方法
     *
     * @param event 玩家克隆事件
     */
    @SubscribeEvent
    public static void onPlayerClone(PlayerEvent.Clone event) {
        Player original = event.getOriginal();
        Player newPlayer = event.getPlayer();

        if (event.isWasDeath()) {
            PlayerDataManager originalData = PlayerDataManager.get(original);
            PlayerDataManager newData = PlayerDataManager.get(newPlayer);

            // 复制所有数据
            newData.deserializeNBT(originalData.serializeNBT());

            // 保存到新玩家
            newData.saveToPlayer(newPlayer);

            // 恢复状态
            //newData.restorePlayerState(newPlayer);
        }
    }

    /**
     * 玩家切换维度事件处理方法
     *
     * @param event 玩家切换维度事件
     */
    @SubscribeEvent
    public static void onPlayerChangedDimension(PlayerEvent.PlayerChangedDimensionEvent event) {
        if (event.getPlayer() instanceof ServerPlayer player) {
            PlayerDataManager dataManager = PlayerDataManager.get(player);
            dataManager.sync(player);
        }
    }

    /**
     * 玩家登出事件处理方法
     *
     * @param event 玩家登出事件
     */
    @SubscribeEvent
    public static void onPlayerLoggedOut(PlayerEvent.PlayerLoggedOutEvent event) {
        Player player = event.getPlayer();
        PlayerDataManager dataManager = PlayerDataManager.get(player);

        // 保存数据
        dataManager.saveToPlayer(player);

        // 清理实例
        PlayerDataManager.clearPlayerData(player.getUUID());
    }
}